import sys,os

inlist = []
for i in os.listdir("./"):
    if i.endswith("fastq.gz") or i.endswith("fq.gz"):
        inlist.append(i)

print(inlist)
inlist.sort()
if not os.path.exists("trim"):
    os.mkdir("trim")
cmd_sh = f'''#!/bin/bash --login
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=1
#SBATCH --cpus-per-task=8
#SBATCH --mem=20G
#SBATCH --job-name=autofastp
#SBATCH --time=40:00:00
#SBATCH --partition=general
#SBATCH --account=a_qaafi_genomics
#SBATCH -o allhic.slurm.out
#SBATCH -e allhic.slurm.error\n
'''
for i in range(0 ,len(inlist), 2):
    print(i)
    outfq1 = inlist[i].split("fq")[0].split("fastq")[0] + "trimmed.fastq.gz"
    outfq2 = inlist[i+1].split("fq")[0].split("fastq")[0] + "trimmed.fastq.gz"

    prefix = inlist[i].split(".")[0]
    print(prefix)
    slum = open(f"{prefix}.slurm", "w")
    cmd = cmd_sh + f"fastp -q 20 -l 75 -w 8 -i {inlist[i]} -I {inlist[i+1]} -o trim/{outfq1} -O trim/{outfq2} -h trim/{prefix}.fastp.html -j trim/{prefix}.fastp.json"
    slum.write(cmd)
    slum.close()
    os.system(f"sbatch {prefix}.slurm")
    
    
